package org.wedding.center.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.wedding.center.entity.RightInfo;
import org.wedding.center.mapper.RightInfoMapper;
import org.wedding.center.service.RightInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wedding.enums.system.ResultCodeEnum;
import org.wedding.utils.CommonResult;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import org.wedding.dto.PageModel;
import org.wedding.utils.PageUtil;

import java.util.List;
/**
 * 权限信息表(RightInfo)表服务实现类
 *
 * @author songkaiyue
 * @since 2025-05-21 14:18:05
 */
@Service
public class RightInfoServiceImpl extends ServiceImpl<RightInfoMapper, RightInfo> implements RightInfoService {
    /**
     * 分页查询所有数据
     * @return
     */
    @Override
    public CommonResult<PageModel> findAll(PageModel query) {
        try {
            List<RightInfo> list = baseMapper.selectAll();
            List<RightInfo> setList = query.setList(PageUtil.getListPage(query.getCurrent(), query.getPageSize(), list), list.size());
            query.setList(setList, list.size());
            return CommonResult.success(query);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }

    }

    /**
     * 根据id查看详情
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<RightInfo> view(String id) {
        try {
            RightInfo rightInfo = baseMapper.findById(id);
            return CommonResult.success(rightInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    @Override
    public CommonResult<RightInfo> findOne() {
        try {
            RightInfo rightInfo = baseMapper.findOne();
            return CommonResult.success(rightInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param rightInfo
     * @return
     */
    @Override
    public CommonResult add(RightInfo rightInfo) {
        try {
            RightInfo info = findOne().getData();
            if (ObjectUtil.isNotEmpty(info)) baseMapper.updateById(info);
            rightInfo.setId(IdUtil.simpleUUID());
            rightInfo.setCreateTime(DateUtil.now());
            rightInfo.setDelFlag("0");
            //默认开关为否
            rightInfo.setIsPrivacy("1");
            rightInfo.setIsAgreement("1");
            rightInfo.setIsDiscount("1");
            //默认比例为1
            rightInfo.setWithdrawalFee("1");
            rightInfo.setCommission("1");
            //默认图片上传最大为10M
            rightInfo.setFileSize("10");
            String userId = StpUtil.getSession().get("userId").toString();
            rightInfo.setCreator(userId);
            baseMapper.insert(rightInfo);
            return CommonResult.success(rightInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 修改数据
     *
     * @param rightInfo
     * @return
     */
    @Override
    public CommonResult updateEntity(RightInfo rightInfo) {
        try {
            if (StrUtil.isEmpty(rightInfo.getId())) return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            rightInfo.setUpdateTime(DateUtil.now());
            baseMapper.updateById(rightInfo);
            return CommonResult.success(rightInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

      /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult delete(List<String> ids) {
        try {
            if (ObjectUtil.isEmpty(ids)) return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            for (String id : ids) {
                baseMapper.upDeleteById(id);
            }
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }
    
}
